texture OriginalMap;
float2 halfPixel;

// Pixel width in texels
float pixelWidth;

float PixelKernel[13] =
{
    -6,
    -5,
    -4,
    -3,
    -2,
    -1,
     0,
     1,
     2,
     3,
     4,
     5,
     6,
};

static const float BlurWeights[13] = 
{
    0.002216,
    0.008764,
    0.026995,
    0.064759,
    0.120985,
    0.176033,
    0.199471,
    0.176033,
    0.120985,
    0.064759,
    0.026995,
    0.008764,
    0.002216,
};

sampler originalSampler = sampler_state
{
    Texture = (OriginalMap);
    AddressU = CLAMP;
    AddressV = CLAMP;
    MagFilter = LINEAR;
    MinFilter = LINEAR;
    Mipfilter = LINEAR;
};

struct VertexShaderInput
{
    float3 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};

struct VertexShaderOutput
{
	float4 Position : POSITION0;
	float2 TexCoord : TEXCOORD0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    output.Position = float4(input.Position,1);
    output.TexCoord = input.TexCoord - halfPixel;

    return output;
}

float4 PixelShaderFunction_X(VertexShaderOutput input) : COLOR0
{

	float4 p = tex2D(originalSampler, input.TexCoord + float2(halfPixel.x * 2.0f, 0)) * 0.25f;
	p += tex2D(originalSampler, input.TexCoord + float2(halfPixel.x * 4.0f, 0)) * 0.25f;
	p += tex2D(originalSampler, input.TexCoord + float2(-halfPixel.x * 2.0f, 0)) * 0.25f;
	p += tex2D(originalSampler, input.TexCoord + float2(-halfPixel.x * 4.0f, 0)) * 0.25f;
	return p;
}

float4 PixelShaderFunction_Y(VertexShaderOutput input) : COLOR0
{

	float4 p = tex2D(originalSampler, input.TexCoord + float2(0, halfPixel.y * 2.0f)) * 0.25f;
	p += tex2D(originalSampler, input.TexCoord + float2(0, halfPixel.y * 4.0f)) * 0.25f;
	p += tex2D(originalSampler, input.TexCoord + float2(0, -halfPixel.y * 2.0f)) * 0.25f;
	p += tex2D(originalSampler, input.TexCoord + float2(0, -halfPixel.y * 4.0f)) * 0.25f;

	return p;
}

// Effect function
float4 GaussBlur( float2 Tex : TEXCOORD0 ) : COLOR0
{
    // Apply surrounding pixels
    float4 color = 0;
    float2 samp = Tex;
    samp.y = Tex.y;

    for (int i = 0; i < 13; i++) {
        samp.x = Tex.x + PixelKernel[i] * pixelWidth;
        color += tex2D(originalSampler, samp.xy) * BlurWeights[i];
    }

    return color;
}

technique BlurLM_X
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction_X();
    }
}
technique BlurLM_Y
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction_Y();
    }
}
technique Gauss
{
    pass Pass1
    {
        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 GaussBlur();
    }
}